﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NewsDiscoveryEngine;
using NewsMine.DomainObjects;
using NewsMine.Storage;

namespace WebApp1.AdminSection
{
    public partial class WebSitesOverview : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (WebApp1.Models.SecurityCheckManager.IsAdminPortalEabled() == false)
            {
                Response.Write("You should be the admin....");
                Response.End();
                return;
            }

            var webSiteKVStore = Defaults.WebSiteInfoMasterStore;

            int start = Convert.ToInt32(this.Request.QueryString.Get("start"));

            int count = webSiteKVStore.GetCount();

            if (count == 0)
                return;

            if (start < 0)
                start = 0;

            #region if domain specific

            string domain = this.Request.QueryString.Get("domain");

            if (!string.IsNullOrEmpty(domain))
            {
                var resultedWebsiteObject = webSiteKVStore.Get(domain);

                if (resultedWebsiteObject != null)
                {
                    WebSiteInfo wbSiteInfo = (WebSiteInfo)resultedWebsiteObject;
                    Response.Write("<table>");
                    RenderWebSiteInfoAsOverview(wbSiteInfo);
                    Response.Write("</table>");
                }
                else
                    Response.Write("invalid domain, please provide proper domain");

                return;
            }

            #endregion



            Response.Write("<h2><a class='btn btn-primary' href='?start=" + (start - 10).ToString() + "'>Previous</a></h2>");
            Response.Write("<h2><a  class='btn btn-primary' href='?start=" + (start + 10).ToString() + "'>Next</a></h2>");
            //todo: website base url need to be taken from the configuration.

            var websiteObjects = webSiteKVStore.Get(start, 10);



            foreach (var websiteInfoObject in websiteObjects)
            {
                Response.Write("<table>");
                WebSiteInfo wbSiteInfo = (WebSiteInfo)websiteInfoObject;

                RenderWebSiteInfoAsOverview(wbSiteInfo);
                Response.Write("</table>");
                Response.Write("<hr/>");
            }


            Response.Write("<h2><a class='btn btn-primary'  href='?start=" + (start - 10).ToString() + "'>Previous</a></h2>");

            Response.Write("<h2><a class='btn btn-primary'  href='?start=" + (start + 10).ToString() + "'>Next</a></h2>");

            Response.Write("<hr/>");
            Response.Write("<h1>Edit specific website</h1>");
            Response.Write("<hr/>");

            var websites = NewsMine.DataManagers.WebSiteInfoManager.GetAllDomains();


            foreach (var wbsite in websites)
            {
                Response.Write("<a style=\"display:inline\" href=\"/adminsection/websitesoverview.aspx?domain=" + wbsite.Domain + "\">"+  wbsite.Domain + "</a>");
                Response.Write("&nbsp;&nbsp;&nbsp;");
            }


        }


        private void RenderWebSiteInfoAsOverview(WebSiteInfo wbSiteInfo)
        {
            Response.Write("<tr>");
            Response.Write("<td>");
            Response.Write("-" + wbSiteInfo.Name);
            Response.Write("</td>");

            Response.Write("<td>");
            Response.Write("-" + wbSiteInfo.Domain);
            Response.Write("</td>");

            Response.Write("<td>");
            Response.Write("<a  class='btn'  target='_blank'   href='" + GetBaseUrlwithApplicationPath() + "/AdminSection/Hintpath.aspx?url=" + "http://" + wbSiteInfo.Domain + "'>Explore Menu Items</a>");
            Response.Write("</td>");

            Response.Write("<td>");
            //Response.Write("<a class='btn'  target='_blank'  href='" + GetBaseUrlwithApplicationPath() + "/AdminSection/WebSiteDetails.aspx?domain=" + wbSiteInfo.Domain + "'>WebSite Details</a>");
            Response.Write("<a class='btn'  target='_blank'  href='" + GetBaseUrlwithApplicationPath() + "/WebSiteInfo/edit/" + wbSiteInfo.Domain + "'>WebSite Details</a>");

            Response.Write("</td>");

            Response.Write("<td>");
            Response.Write("<a target=\"_blank\" class=\"btn btn-warning\" href=\"/WebSiteInfo/Delete/" + wbSiteInfo.Domain + "\">Delete " + wbSiteInfo.Domain + "</a>");
            Response.Write("</td>");


            Response.Write("<td>");

            Response.Write("<div class=\"btn-group\">");
            Response.Write("<button class=\"btn btn-info dropdown-toggle\" data-toggle=\"dropdown\"> Tag me <span class=\"caret\"></span></button>");
            Response.Write("<ul class=\"dropdown-menu\">");
            Response.Write("<li><a target=\"_blank\"  href=\"/WebSiteInfo/TagMenu/" + wbSiteInfo.Domain + "?tag=LatestNews" + "\">LatestNews</a></li>");

            Response.Write("<li><a target=\"_blank\"  href=\"/WebSiteInfo/TagMenu/" + wbSiteInfo.Domain + "?tag=TopStories" + "\">TopStories</a></li>");

            Response.Write("<li><a target=\"_blank\"  href=\"/WebSiteInfo/TagMenu/" + wbSiteInfo.Domain + "?tag=India" + "\">India</a></li>");
            Response.Write("<li><a target=\"_blank\"  href=\"/WebSiteInfo/TagMenu/" + wbSiteInfo.Domain + "? tag=Regional" + "\">Regional</a></li>");
            Response.Write("<li><a target=\"_blank\"  href=\"/WebSiteInfo/TagMenu/" + wbSiteInfo.Domain + "?tag=Business" + "\">Business</a></li>");
            Response.Write("<li><a target=\"_blank\"  href=\"/WebSiteInfo/TagMenu/" + wbSiteInfo.Domain + "?tag=Entertainment" + "\">Entertainment</a></li>");
            Response.Write("<li><a target=\"_blank\"  href=\"/WebSiteInfo/TagMenu/" + wbSiteInfo.Domain + "?tag=Health" + "\">Health</a></li>");
            Response.Write("<li><a target=\"_blank\"  href=\"/WebSiteInfo/TagMenu/" + wbSiteInfo.Domain + "?tag=LifeStyle" + "\">LifeStyle</a></li>");
            Response.Write("<li><a  target=\"_blank\" href=\"/WebSiteInfo/TagMenu/" + wbSiteInfo.Domain + "?tag=Sports" + "\">Sports</a></li>");
            Response.Write("<li><a target=\"_blank\"  href=\"/WebSiteInfo/TagMenu/" + wbSiteInfo.Domain + "?tag=World" + "\">World</a></li>");
            Response.Write("<li><a target=\"_blank\"  href=\"/WebSiteInfo/TagMenu/" + wbSiteInfo.Domain + "?tag=SciTech" + "\">SciTech</a></li>");
            Response.Write("<li><a target=\"_blank\"  href=\"/WebSiteInfo/TagMenu/" + wbSiteInfo.Domain + "?tag=AutoMobiles" + "\">AutoMobiles</a></li>");
            Response.Write("<li><a target=\"_blank\"  href=\"/WebSiteInfo/TagMenu/" + wbSiteInfo.Domain + "?tag=Local" + "\">Local</a></li>");
            Response.Write("<li class=\"divider\"></li>");
            Response.Write("<li><a target=\"_blank\"  href=\"#\">AddNewTag not working</a></li>");

            Response.Write("</ul>");
            Response.Write("</div>");
            Response.Write("</td>");

            Response.Write("<td>");

            if (wbSiteInfo.Tags != null)
            {
                foreach (var tg in wbSiteInfo.Tags)
                {
                    Response.Write("<span class=\"label\">" + tg + "</span> , ");
                }
            }
            Response.Write("</td>");



            if (wbSiteInfo.MenuItemconfigs == null || wbSiteInfo.MenuItemconfigs.Count == 0)
                return;

            Response.Write("<td>");
            Response.Write("domain:" + wbSiteInfo.Domain);
            Response.Write("</td>");


            Response.Write("</tr></table><table><tr><td>");
            Response.Write("<br/>");
            foreach (var mnuItem in wbSiteInfo.MenuItemconfigs)
            {
                //Response.Write("<br/><b>" + mnuItem.Title + "</b>" + " - <i>" + mnuItem.Url + "</i>");
                Response.Write("&nbsp;<br/><br/>");
                Response.Write("<a  class='btn' target='_blank'  href='" + GetBaseUrlwithApplicationPath() + "/AdminSection/WebSiteMenuDetails.aspx?url=" + mnuItem.Url + "&domain=" + wbSiteInfo.Domain + "'>" + mnuItem.Title + "</a>");
                Response.Write("<a target=\"_blank\" class=\"btn btn-warning\" href=\"/WebSiteInfo/DeleteMenu/" + wbSiteInfo.Domain + "?menuurl=" + HttpUtility.UrlEncodeUnicode(mnuItem.Url) + "\">X" + "</a>");

                Response.Write(mnuItem.Url);


               

                Response.Write("<div class=\"btn-group\">");
                Response.Write("<button class=\"btn btn-info dropdown-toggle\" data-toggle=\"dropdown\"> Tag me <span class=\"caret\"></span></button>");
                Response.Write("<ul class=\"dropdown-menu\">");
                Response.Write("<li><a target=\"_blank\"  href=\"/WebSiteInfo/TagMenu/" + wbSiteInfo.Domain + "?menuurl=" + HttpUtility.UrlEncodeUnicode(mnuItem.Url) + "&tag=LatestNews" + "\">LatestNews</a></li>");

                Response.Write("<li><a target=\"_blank\"  href=\"/WebSiteInfo/TagMenu/" + wbSiteInfo.Domain + "?menuurl=" + HttpUtility.UrlEncodeUnicode(mnuItem.Url) + "&tag=TopStories" + "\">TopStories</a></li>");

                Response.Write("<li><a target=\"_blank\"  href=\"/WebSiteInfo/TagMenu/" + wbSiteInfo.Domain + "?menuurl=" + HttpUtility.UrlEncodeUnicode(mnuItem.Url) + "&tag=India" + "\">India</a></li>");
                Response.Write("<li><a target=\"_blank\"  href=\"/WebSiteInfo/TagMenu/" + wbSiteInfo.Domain + "?menuurl=" + HttpUtility.UrlEncodeUnicode(mnuItem.Url) + "&tag=Regional" + "\">Regional</a></li>");
                Response.Write("<li><a target=\"_blank\"  href=\"/WebSiteInfo/TagMenu/" + wbSiteInfo.Domain + "?menuurl=" + HttpUtility.UrlEncodeUnicode(mnuItem.Url) + "&tag=Business" + "\">Business</a></li>");
                Response.Write("<li><a target=\"_blank\"  href=\"/WebSiteInfo/TagMenu/" + wbSiteInfo.Domain + "?menuurl=" + HttpUtility.UrlEncodeUnicode(mnuItem.Url) + "&tag=Entertainment" + "\">Entertainment</a></li>");
                Response.Write("<li><a target=\"_blank\"  href=\"/WebSiteInfo/TagMenu/" + wbSiteInfo.Domain + "?menuurl=" + HttpUtility.UrlEncodeUnicode(mnuItem.Url) + "&tag=Health" + "\">Health</a></li>");
                Response.Write("<li><a target=\"_blank\"  href=\"/WebSiteInfo/TagMenu/" + wbSiteInfo.Domain + "?menuurl=" + HttpUtility.UrlEncodeUnicode(mnuItem.Url) + "&tag=LifeStyle" + "\">LifeStyle</a></li>");
                Response.Write("<li><a  target=\"_blank\" href=\"/WebSiteInfo/TagMenu/" + wbSiteInfo.Domain + "?menuurl=" + HttpUtility.UrlEncodeUnicode(mnuItem.Url) + "&tag=Sports" + "\">Sports</a></li>");
                Response.Write("<li><a target=\"_blank\"  href=\"/WebSiteInfo/TagMenu/" + wbSiteInfo.Domain + "?menuurl=" + HttpUtility.UrlEncodeUnicode(mnuItem.Url) + "&tag=World" + "\">World</a></li>");
                Response.Write("<li><a target=\"_blank\"  href=\"/WebSiteInfo/TagMenu/" + wbSiteInfo.Domain + "?menuurl=" + HttpUtility.UrlEncodeUnicode(mnuItem.Url) + "&tag=SciTech" + "\">SciTech</a></li>");
                Response.Write("<li><a target=\"_blank\"  href=\"/WebSiteInfo/TagMenu/" + wbSiteInfo.Domain + "?menuurl=" + HttpUtility.UrlEncodeUnicode(mnuItem.Url) + "&tag=AutoMobiles" + "\">AutoMobiles</a></li>");
                Response.Write("<li><a target=\"_blank\"  href=\"/WebSiteInfo/TagMenu/" + wbSiteInfo.Domain + "?menuurl=" + HttpUtility.UrlEncodeUnicode(mnuItem.Url) + "&tag=Local" + "\">Local</a></li>");
                Response.Write("<li class=\"divider\"></li>");
                Response.Write("<li><a target=\"_blank\"  href=\"#\">AddNewTag not working</a></li>");

                Response.Write("</ul>");
                Response.Write("</div>");

                Response.Write("<br/>");


                if (mnuItem.Tags != null)
                {
                    foreach (var tg in mnuItem.Tags)
                    {
                        Response.Write("<span class=\"label\">" + tg + "</span> , ");
                    }
                }


            }

            Response.Write("</td>");

            Response.Write("</tr></table>");



        }

        private string GetBaseUrlwithApplicationPath()
        {
            string rawUrl = this.Request.RawUrl;

            return rawUrl.ToLower().Substring(0, rawUrl.ToLower().IndexOf("/adminsection/"));

            //            Request.RawUrl(
            //"/webapp1/AdminSection/WebSitesOverview.aspx"
        }
    }
}